﻿
String.prototype.trim = function () {
    return this.replace(/^\s+/g, "").replace(/\s+$/g, "");
}

function ischinese(str) //判断是否有中文
{
    var reg = /[^A-Za-z0-9_]/g;
    if (reg.test(str)) {
        return true;
    }
    else {
        return false;
    }
}

function ValCodeInput() {
    if (event.keyCode == 32)
        event.returnValue = null;
    if (event.keyCode == 13) {
        $("#btnLogin").click();
    }
}

function changeRole(obj) {
    switch (obj) {
        case "0":
        case "1":
        case "2":
            $("#divUser").show();
            $("#divSub").hide();
            $("#divJSDW").hide();
            $("#divCard").hide();
            break;
        case "3":
            $("#divSub").show();
            $("#dpSubRole option[value='1']").text("项目负责人");
            $("#dpSubRole ").val('0');
            $("#divJSDW").show();
            $("#divUser").hide();
            $("#divCard").hide();
            break;
        case "4":
        case "5":
            $("#divSub").show();
            if (obj == '4') {
                $("#dpSubRole option[value='1']").text("总监理工程师");
            }
            else {
                $("#dpSubRole option[value='1']").text("注册建造师");
            }
            $("#dpSubRole ").val('0');
            $("#divCard").show();
            $("#divUser").hide();
            $("#divJSDW").hide();
            break;
        default:
            break;
    }
    $("#error_msg").text("");
}
function changeSubRole(obj) {
    if ($("#dpUserRole").val() != '3') {
        switch (obj) {
            case "0":
                $("#divJSDW").hide();
                $("#divUser").hide();
                $("#divCard").show();
                break;

            case "1":
            case "2":
                $("#divJSDW").hide();
                $("#divUser").show();
                $("#divCard").hide();
                break;

            default:
                break;
        }
    }
    else {
        switch (obj) {
            case "0":
                $("#divJSDW").show();
                $("#divUser").hide();
                $("#divCard").hide();
                break;
            case "1":
            case "2":
                $("#divJSDW").hide();
                $("#divUser").show();
                $("#divCard").hide();
                break;
            default:
                break;
        }
    }
    $("#error_msg").text("");
}

function LoginIt() {
    $("#error_msg").text("");
    var uselectrole = $("#dpUserRole").val();

    switch (uselectrole) {
        case "0":
        case "1":
        case "2":
            CallLogin(uselectrole);
            break;
        case "3":
            if ($("#dpSubRole").val() == "0") {
                JianSheDengLu();
            }
            else if ($("#dpSubRole").val() == "1") {
                CallLogin(uselectrole + '1');
            }
            else if ($("#dpSubRole").val() == "2") {
                CallLogin(uselectrole + '11');
            }
            break;
        case "4":
        case "5":
            if ($("#dpSubRole").val() == "0") {
                CardLogin(uselectrole);
            }
            else if ($("#dpSubRole").val() == "1") {
                CallLogin(uselectrole + '1');
            }
            else if ($("#dpSubRole").val() == "2") {
                CallLogin(uselectrole + '11');
            }
            break;
        default: break;
    }
}

function CallLogin(userrole) {
    if (CheckLogIn() == true) {
        $("#btnLogin").disable = true;
        $("#btnLogin").attr("className", "BtnLogin_In");

        var username = $("#txtUserName").val().trim();
        var password = $("#txtUserPwd").val().trim();
        var valcode = $("#txtUserVal").val().trim();
        
        $.ajax({
            url: "Ashx/UserLogin.ashx",
            type: "post",
            data: {
                UserName: username,
                Password: password,
                ValCode: valcode,
                UserRole: userrole,
                dlType: 1
            },
            dataType: "text",
            success: function (json) {
                var errMsg = json.split("###")[0];
                try {
                    var jpUrl = json.split("###")[1];
                }
                catch (e) { }
                if (errMsg != "") {
                    $("#valcode").click();
                    $("#error_msg").html(errMsg);
                    $("#btnLogin").disable = false;
                    $("#btnLogin").attr("className", "BtnLogin");
                }
                else {
                    $("#btnLogin").disable = false;
                    window.location.href = jpUrl;
                }
            }
        });
    }
}
function JianSheDengLu() {
    if (CheckJianShe() == true) {
        $("#btnLogin").disable = true;
        $("#btnLogin").attr("className", "BtnLogin_In");

        var username = $("#JSID").val().trim();
        var areacode = $("#AreaCode").text().trim(); 
        var password = $("#txtKouLing").val().trim();
        $.ajax({
            url: "Ashx/UserLogin.ashx",
            type: "post",
            data: {
                UserName: username,
                AreaCode: areacode,
                Password: password,
                UserRole: 3,
                dlType: 3
            },
            dataType: "text",
            success: function (json) {
                var errMsg = json.split("###")[0];
                try {
                    var jpUrl = json.split("###")[1];
                }
                catch (e) { }
                if (errMsg != "") {
                    $("#valcode").click();
                    $("#error_msg").html(errMsg);
                    $("#btnLogin").disable = false;
                    $("#btnLogin").attr("className", "BtnLogin");
                }
                else {
                    $("#btnLogin").disable = false;
                    window.location.href = jpUrl;
                }
            }
        });
    }
}

function CardLogin(userrole) {
    try {
        var comActiveX = new ActiveXObject("Iccard.icRead.1");
    }
    catch (e) {
        $("#error_msg").text('您还没有安装读卡器驱动，或驱动安装不正确，请确定安装驱动的时候运行了第二步"注册控件"。请到www.bcactc.com下载读卡器驱动！');
        return false;
    }

    var psRet

    psRet = wcard1.ReturnSN();

    if (ischinese(psRet)) {
        $("#error_msg").text("请插IC卡登录！");
        return false;
    }

    $("#btnLogin").attr("className", "BtnLogin_In");

    $.ajax({
        url: "Ashx/UserLogin.ashx",
        type: "post",
        data:
        {
            UserRole: userrole,
            UserName: psRet
        },
        dataType: "text",
        success: function (json) {
            var errMsg = json.split("###")[0];
            try {
                var jpUrl = json.split("###")[1];
            }
            catch (e) { }
            if (errMsg != "") {
                $("#valcode").click();
                $("#error_msg").text(errMsg);
                $("#btnLogin").disable = false;
                $("#btnLogin").attr("className", "BtnLogin");
            }
            else {
                $("#btnLogin").disable = false;
                window.location.href = jpUrl;
            }
        }
    });
}

function CheckJianShe() {
    var unam = $("#JSID").val().trim();
    var upwd = $("#txtKouLing").val().trim();
    if (unam.length == 0) {
        $("#error_msg").html("请选择建设单位！");
        return false;
    }
    if (upwd.length == 0) {
        $("#error_msg").html("口令不能为空格或空！");
        $("#txtKouLing").focus();
        return false;
    }
    return true;
}


function CheckLogIn() {
    var unam = $("#txtUserName").val().trim();
    var upwd = $("#txtUserPwd").val().trim();
    var uval = $("#txtUserVal").val().trim();
    if (unam.length == 0) {
        $("#error_msg").html("用户名不能为空！");
        $("#txtUserName").focus();
        return false;
    }
    //    else if (unam.length < 5 || unam.length > 20) {
    //        $("#error_msg").html("您输入的用户名格式有误！");
    //        $("#txtUserName").focus();
    //        return false;
    //    }
    if (upwd.length == 0) {
        $("#error_msg").html("密码不能为空格或空！");
        $("#txtUserPwd").focus();
        return false;
    }
    else if (upwd.length < 4) {
        $("#error_msg").html("您输入的密码格式有误！");
        $("#txtUserPwd").focus();
        return false;
    }
    else if (upwd.length >= 100) {
        $("#error_msg").html("您试图输入过长的密码！");
        $("#txtUserPwd").focus();
        return false;
    }
    if (uval.length < 4) {
        $("#error_msg").html("请输入正确的验证码！");
        $("#txtUserVal").focus();
        return false;
    }
    else if (uval.length > 4) {
        $("#error_msg").html("您输入的验证码有误！");
        $("#txtUserVal").focus();
        return false;
    }
    else if (uval.length == 4) {
        var reg = /^\d+$/;
        if (reg.test(uval)) {
            return true;
        }
        else {
            $("#error_msg").html("您输入的验证码格式有误！");
            $("#txtUserVal").focus();
            return false;
        }
    }
    return true;
}
